﻿package mortal.game.scene3D.fight.dramaEffect
{
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import com.gengine.core.frame.*;
    import com.gengine.utils.pools.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.pools.*;
    import mortal.game.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.fight.dramaEffect.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.scene3D.model.pools.*;
    import mortal.game.scene3D.player.entity.*;

    public class DramaEffect extends Object
    {
        private var _dramaEffectInfo:DramaEffectInfo;
        private var _aryMoveData:Array;
        private var _timer:FrameTimer;
        private var _callBack:Function;
        private var _aryPlayer:Array;
        private var _stopEffect:EffectPlayer;

        public function DramaEffect(param1:DramaEffectInfo, param2:Function)
        {
            this._aryPlayer = [];
            this._dramaEffectInfo = param1;
            this._callBack = param2;
            this.init();
            return;
        }// end function

        private function init() : void
        {
            this.alzData();
            if (this._dramaEffectInfo.delay > 0)
            {
                setTimeout(this.start, this._dramaEffectInfo.delay);
            }
            else
            {
                this.start();
            }
            return;
        }// end function

        private function start() : void
        {
            if (this._dramaEffectInfo.repeatCount > 1 || this._dramaEffectInfo.repeatCount == -1)
            {
                if (this._dramaEffectInfo.repeatCount == -1)
                {
                    if (this._callBack != null)
                    {
                        this._callBack.call();
                    }
                }
                this.addFrameTimer();
            }
            else if (this._dramaEffectInfo.effectType == DramaEffectType.EffectTypeMove)
            {
                this.move(true);
            }
            else if (this._dramaEffectInfo.effectType == DramaEffectType.EffectTypeSkill)
            {
                this.fireSkill(true);
            }
            return;
        }// end function

        private function alzData() : void
        {
            var _loc_1:int = 0;
            var _loc_2:DramaMoveData = null;
            var _loc_3:Point = null;
            var _loc_4:Point = null;
            this._aryMoveData = [];
            if (this._dramaEffectInfo.effectType == DramaEffectType.EffectTypeMove)
            {
                _loc_1 = 0;
                while (_loc_1 < (this._dramaEffectInfo.points.length - 1))
                {
                    
                    if (this._dramaEffectInfo.moveTime.length > _loc_1 && this._dramaEffectInfo.moveType.length > _loc_1 && this._dramaEffectInfo.scale.length > (_loc_1 + 1))
                    {
                        _loc_2 = new DramaMoveData();
                        _loc_3 = this._dramaEffectInfo.points[_loc_1] as Point;
                        _loc_4 = this._dramaEffectInfo.points[(_loc_1 + 1)] as Point;
                        _loc_2.startX = _loc_3.x;
                        _loc_2.startY = _loc_3.y;
                        _loc_2.endX = _loc_4.x;
                        _loc_2.endY = _loc_4.y;
                        _loc_2.startScale = this._dramaEffectInfo.scale[_loc_1];
                        _loc_2.endScale = this._dramaEffectInfo.scale[(_loc_1 + 1)];
                        _loc_2.moveType = this._dramaEffectInfo.moveType[_loc_1];
                        _loc_2.moveTime = this._dramaEffectInfo.moveTime[_loc_1];
                        this._aryMoveData.push(_loc_2);
                    }
                    _loc_1++;
                }
            }
            return;
        }// end function

        private function addFrameTimer() : void
        {
            var _loc_1:* = this._dramaEffectInfo.repeatCount;
            if (_loc_1 == -1)
            {
                _loc_1 = int.MAX_VALUE;
            }
            this._timer = new FrameTimer(this._dramaEffectInfo.repeatInterval, _loc_1);
            this._timer.addListener(TimerType.ENTERFRAME, this.onEnterFrame);
            this._timer.start();
            return;
        }// end function

        private function onEnterFrame(param1:FrameTimer) : void
        {
            var _loc_2:* = param1.repeatCount == 0;
            if (this._dramaEffectInfo.effectType == DramaEffectType.EffectTypeMove)
            {
                this.move(_loc_2);
            }
            else if (this._dramaEffectInfo.effectType == DramaEffectType.EffectTypeSkill)
            {
                this.fireSkill(_loc_2);
            }
            return;
        }// end function

        public function move(param1:Boolean = false) : void
        {
            var effectPlayer:Pivot3D;
            var playEnd:Function;
            var dramaMoveData:DramaMoveData;
            var isLast:* = param1;
            playEnd = function () : void
            {
                _aryPlayer = _aryPlayer.splice(_aryPlayer.indexOf(effectPlayer), 1);
                effectPlayer.dispose();
                if (isLast)
                {
                    if (_callBack != null)
                    {
                        _callBack.call();
                    }
                    dispose();
                }
                return;
            }// end function
            ;
            if (!this._dramaEffectInfo.isXML)
            {
                effectPlayer = EffectPlayerPool.instance.getEffectPlayer(this._dramaEffectInfo.src, Game.scene.playerLayer, null, true);
                (effectPlayer as EffectPlayer).game3DControler = Game.scene.gameControler;
            }
            else
            {
                effectPlayer = FrObjectPool.getObject(GroupEffectPlayer, this);
                (effectPlayer as GroupEffectPlayer).load(this._dramaEffectInfo.src, true);
            }
            (effectPlayer as IGame2D).play(PlayMode.ANIMATION_LOOP_MODE);
            this._aryPlayer.push(effectPlayer);
            if (this._aryMoveData.length > 0)
            {
                dramaMoveData = this._aryMoveData[0] as DramaMoveData;
                (effectPlayer as IGame2D).x2d = dramaMoveData.startX;
                (effectPlayer as IGame2D).y2d = dramaMoveData.startY;
                (ObjectPool.getObject(DramaEffectArray) as DramaEffectArray).play(effectPlayer, this._aryMoveData, playEnd);
            }
            return;
        }// end function

        public function fireSkill(param1:Boolean = false) : void
        {
            var _loc_3:Point = null;
            var _loc_4:Pivot3D = null;
            var _loc_2:int = 0;
            while (_loc_2 < this._dramaEffectInfo.points.length)
            {
                
                _loc_3 = this._dramaEffectInfo.points[_loc_2] as Point;
                if (!this._dramaEffectInfo.isXML)
                {
                    _loc_4 = EffectPlayerPool.instance.getEffectPlayer(this._dramaEffectInfo.src, Game.scene.playerLayer, null, true);
                    (_loc_4 as EffectPlayer).game3DControler = Game.scene.gameControler;
                }
                else
                {
                    _loc_4 = FrObjectPool.getObject(GroupEffectPlayer, this);
                    (_loc_4 as GroupEffectPlayer).load(this._dramaEffectInfo.src, true, null, Game.scene.playerLayer);
                }
                if (this._dramaEffectInfo.scale.length > _loc_2)
                {
                    _loc_4.scaleX = this._dramaEffectInfo.scale[_loc_2];
                    _loc_4.scaleY = this._dramaEffectInfo.scale[_loc_2];
                    _loc_4.scaleZ = this._dramaEffectInfo.scale[_loc_2];
                }
                (_loc_4 as IGame2D).x2d = _loc_3.x;
                (_loc_4 as IGame2D).y2d = _loc_3.y;
                (_loc_4 as IGame2D).play(PlayMode.ANIMATION_STOP_MODE);
                _loc_2++;
            }
            if (param1)
            {
                if (this._callBack != null)
                {
                    this._callBack.call();
                }
                this.dispose();
            }
            return;
        }// end function

        public function removeAllChildren() : void
        {
            var _loc_2:Pivot3D = null;
            var _loc_1:int = 0;
            while (_loc_1 < this._aryPlayer.length)
            {
                
                _loc_2 = this._aryPlayer[_loc_1] as Pivot3D;
                if (_loc_2)
                {
                    _loc_2.dispose();
                }
                _loc_1++;
            }
            return;
        }// end function

        public function dispose() : void
        {
            if (this._timer)
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            this._aryMoveData = [];
            this._callBack = null;
            this._dramaEffectInfo = null;
            var _loc_1:* = this._aryPlayer.length;
            var _loc_2:int = 0;
            while (_loc_2 < this._aryPlayer.length)
            {
                
                (this._aryPlayer[_loc_2] as Pivot3D).dispose();
                _loc_2++;
            }
            this._aryPlayer = [];
            return;
        }// end function

        public function get dramaEffectInfo() : DramaEffectInfo
        {
            return this._dramaEffectInfo;
        }// end function

    }
}
